﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace MoviesManager
{
    class HelperClass
    {
        public static string ToTitleCase(string str)
        {
            string result = str;
            if (!string.IsNullOrEmpty(str))
            {
                var words = str.Split(' ');
                for (int index = 0; index < words.Length; index++)
                {
                    var s = words[index];
                    if (s.Length > 0)
                    {
                        words[index] = s[0].ToString().ToUpper() + s.Substring(1);
                    }
                }
                result = string.Join(" ", words);
            }
            return result;
        }

        static public void CopyFolder(string sourceFolder, string destFolder)
        {
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);

            string[] folders = Directory.GetDirectories(sourceFolder);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(destFolder, name);
                Directory.CreateDirectory(dest);
            }
        }

        public static bool SaveMovieCollectionToConfig(VMovie theMovie)
        {
            XmlSerializer xser = new XmlSerializer(typeof(VMovie));
            MemoryStream m = new MemoryStream();
            try
            {
                xser.Serialize(m, theMovie);
                File.WriteAllBytes(Path.Combine(theMovie.FullPath, "vmconf"), m.GetBuffer());

            }
            catch
            {
                m.Close();
                return false;
            }
            m.Close();
            return true;
        }

        public static void LoadMovieFromConf(ref VMovie theMovie, FileInfo fi)
        {
            XmlSerializer xser = new XmlSerializer(typeof(VMovie));
            if (File.Exists(fi.FullName))
            {
                var theStream = File.Open(fi.FullName, FileMode.Open, FileAccess.Read);
                theMovie = (VMovie)xser.Deserialize(theStream);
                theStream.Close();
            }
        }
    }
}
